package com.his.server.main;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import com.opensymphony.xwork2.ActionSupport
;
import com.his.server.consts.ServerConsts;
import com.his.server.utils.main.BaseContext;
import com.his.server.utils.main.EnvironmentProperties;
import com.his.server.logicdata.BaseLogicData;
import com.his.server.logicdata.UserInfoLogicData;

public class MainRequestAction extends ActionSupport {

	    HttpServletRequest request = BaseContext.getServletRequest();     
        BaseLogicData baseLogicData = new BaseLogicData();
        UserInfoLogicData userInfoLogicData = new UserInfoLogicData();
         
        @Override
        public String execute() throws Exception {
        	
                return super.execute();
        }
        
        // patch request_type
        public String requestPatch()
        {       
//        	List <Properties> a = EnvironmentProperties.getEproperties().getPropertyList();
//        	String bb = a.get(0).getProperty("db_driver_name");
//        	String aa = a.get(1).getProperty("a");
        			
        	    //get HttpServletRequest
                String[] request_type = (String[]) BaseContext.getParameter("remote_request");
                
                if (request_type == null || request_type.length < 1) {
                	
                	baseLogicData.setRequestflag(ServerConsts.REQUEST_FAILURE);
                	
                	// set baseLogicdata
                	BaseContext.setAtribute(ServerConsts.SK_BASELOGICDATA, baseLogicData);
                    return ServerConsts.REQUEST_FAILURE;
                } else {
                	
                	// request_type connect
                    if (StringUtils.equals(request_type[0], ServerConsts.REQUEST_CONNECT)) {

                    	baseLogicData.setRequestflag(ServerConsts.REQUEST_SUCCESS);
                    	baseLogicData.setMethodname(ServerConsts.REQUEST_CONNECT);
                    	
                    	// set baseLogicdata
                    	BaseContext.setAtribute(ServerConsts.SK_BASELOGICDATA, baseLogicData);
                    	
                        return ServerConsts.REQUEST_SUCCESS;
                    
                    // request_type login     
                    } else if (StringUtils.equals(request_type[0], ServerConsts.REQUEST_LOGIN)) {
                    	
                    	baseLogicData = (BaseLogicData) BaseContext.getAtribute(ServerConsts.SK_BASELOGICDATA);
                		
                    	if (baseLogicData != null && 
                    			StringUtils.equals(baseLogicData.getRequestflag(), ServerConsts.REQUEST_SUCCESS)) {
                    		
                    		baseLogicData.setMethodname(ServerConsts.REQUEST_LOGIN);
                    		
                        	String[] user_info = (String[]) BaseContext.getParameter("user_info");
                        	
                        	if (user_info != null && user_info.length >= 2) {
                        		
                        		userInfoLogicData.setName(user_info[0]);
                        		userInfoLogicData.setPassword(user_info[1]);
                        	}
                    		
                        	// set baseLogicdata
                        	BaseContext.setAtribute(ServerConsts.SK_BASELOGICDATA, baseLogicData);
                        	BaseContext.setAtribute(ServerConsts.SK_USERINFOLOGICDATA, userInfoLogicData);
                        	
                            return ServerConsts.REQUEST_LOGIN_SUCCESS;
                    	} else {
                        	
                    		return ServerConsts.REQUEST_FAILURE;
                    	}

                    }
                }
                
                return ServerConsts.SUCCESS;

        }
}